﻿using System;
using System.Linq;

namespace DBProvider.Providers
{
	public class CollegeProvider:ICollegeProvider
	{
		#region ICollegeProvider Members

		public College CreateCollege(College theNewCollege, out string msg)
		{
			if(theNewCollege == null)
			{
				throw new ArgumentNullException();
			}

			msg = string.Empty;
			College result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.Colleges.Count(c => c.CollegeName == theNewCollege.CollegeName) > 0;

					if(!isExist)
					{
						db.Colleges.InsertOnSubmit(theNewCollege);
						db.SubmitChanges();

						result = db.Colleges.SingleOrDefault(c => c.CollegeName == theNewCollege.CollegeName);
					}
					else
					{
						msg = "同名的学院已存在";
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateCollege(College theCollege)
		{
			if(theCollege == null || theCollege.CollegeID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbCollege = db.Colleges.SingleOrDefault(c => c.CollegeID == theCollege.CollegeID);

					if(dbCollege != null)
					{
						dbCollege.CollegeDescription = theCollege.CollegeDescription;

						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteCollege(int collegeID)
		{
			if(collegeID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbCollege = db.Colleges.SingleOrDefault(c => c.CollegeID == collegeID);

					if(dbCollege != null)
					{
						db.Colleges.DeleteOnSubmit(dbCollege);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public College GetCollege(int CollegeID)
		{
			if(CollegeID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			College result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.Colleges.SingleOrDefault(c => c.CollegeID == CollegeID);
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public College GetCollege(string collegeName)
		{
			if(string.IsNullOrEmpty(collegeName))
			{
				throw new ArgumentException();
			}

			College result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.Colleges.SingleOrDefault(c => c.CollegeName.Equals(collegeName));
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		#endregion
	}
}
